package com.parkinglot.biz;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public abstract class Statistics {


    public void execute(StatisticsContext context){

        generateDate(context);

        statistics(context);

    }

    protected abstract void statistics(StatisticsContext context);


    protected  void generateDate(StatisticsContext context){
        if (context.getDateTypeEnum()==null) throw new RuntimeException("类型不能为空!");

        LocalDate today = LocalDate.now();
        switch (context.getDateTypeEnum()) {
            case YESTERDAY:
                context.setBeginDate(LocalDate.now().plusDays(-1));
                context.setEndDate(context.getBeginDate());
                break;
            case LASTWEEK:
                context.setBeginDate(today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
                context.setEndDate(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)));
                break;
            case LASTMONTH:
                context.setBeginDate(LocalDate.of(today.getYear(), today.getMonthValue() - 1, 1));
                context.setEndDate(context.getBeginDate().with(TemporalAdjusters.lastDayOfMonth()));
                break;
            default:
                throw new RuntimeException("暂不支持此类型!");
        }
    }

}
